package com.neudu.yiyang.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.neudu.yiyang.commons.JSONReturn;
import com.neudu.yiyang.pojo.Basicdata;
import com.neudu.yiyang.pojo.Beddetails;
import com.neudu.yiyang.pojo.Customer;
import com.neudu.yiyang.service.BeddetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class BedManage {
    @Autowired
    private BeddetailsService beddetailsService;
    @Autowired
    private JSONReturn jsonReturn;

    @RequestMapping("get_all_beds")
    public String getAll(@RequestBody PageInfo pageInfo) {
        try {
            PageHelper.startPage(pageInfo.getPageNum(), pageInfo.getPageSize());
            List<Beddetails> list = beddetailsService.list();
            PageInfo<Beddetails> pageInfoReturn = new PageInfo<>(list);
            return jsonReturn.returnSuccess(pageInfoReturn);
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

    @RequestMapping("get_bed_by")
    public String getByCondition(Beddetails beddetails, PageInfo pageInfo){
        try {
            PageHelper.startPage(pageInfo.getPageNum(),pageInfo.getPageSize());
            LambdaQueryWrapper<Beddetails> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(!ObjectUtils.isEmpty(beddetails.getId()), Beddetails::getId, beddetails.getId())
                    .eq(!ObjectUtils.isEmpty(beddetails.getCustomerName()), Beddetails::getCustomerName, beddetails.getCustomerName())
                    .eq(!ObjectUtils.isEmpty(beddetails.getStartDate()), Beddetails::getStartDate, beddetails.getStartDate())
                    .eq(!ObjectUtils.isEmpty(beddetails.getEndDate()), Beddetails::getEndDate, beddetails.getEndDate())
                    .eq(!ObjectUtils.isEmpty(beddetails.getBedDetails()), Beddetails::getBedDetails, beddetails.getBedDetails())
                    .eq(!ObjectUtils.isEmpty(beddetails.getBedId()), Beddetails::getBedId, beddetails.getBedId());
            List<Beddetails> beddetails1 = beddetailsService.list(queryWrapper);
            PageInfo<Beddetails> pageInfo1 = new PageInfo<>(beddetails1);
            return jsonReturn.returnSuccess(pageInfo1);
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }


    @RequestMapping("add_one_bed")
    public String addOne(@RequestBody Beddetails beddetails){
        try {
            beddetailsService.save(beddetails);
            return jsonReturn.returnSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

    @RequestMapping("getbedby_id")
    public String getById(@RequestParam("bedid") Integer bedid){
        try {
            Beddetails beddetails = beddetailsService.getById(bedid);
            return jsonReturn.returnSuccess(beddetails);
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

    @RequestMapping("modifybedby_id")
    public String modifyById(@RequestBody Beddetails beddetails){
        try {
            beddetailsService.updateById(beddetails);
            return jsonReturn.returnSuccess(beddetails);
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

    @RequestMapping("removebed_by_id")
    public String removeByid(@RequestParam("bedid") Integer bedid) {
        try {
            beddetailsService.removeById(bedid);
            return jsonReturn.returnSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }
}
